package com.example.mystoryapp2.ui.story

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.lifecycle.ViewModelProvider
import com.example.mystoryapp2.R
import com.example.mystoryapp2.contexts.AppContext
import com.example.mystoryapp2.contexts.StoryContext
import com.example.mystoryapp2.databinding.ActivityStoryDescriptionLayoutBinding
import com.example.mystoryapp2.ui.base.BaseActivity
import com.example.mystoryapp2.ui.story.create.CreateStoryActivity
import com.example.mystoryapp2.ui.story.page.PageActivity
import com.example.mystoryapp2.ui.utils.DialogUtils
import com.example.mystoryapp2.ui.utils.RecycleViewUtils

class StoryDescriptionActivity : BaseActivity<ActivityStoryDescriptionLayoutBinding,
        StoryDescriptionViewModel>() {
    companion object {
        fun start(ctx: Context, id: Int) {
            StoryContext.storyId = id
            startActivity(ctx)
        }

        private fun startActivity(ctx: Context) {
            val intent = Intent(ctx, StoryDescriptionActivity::class.java)
            ctx.startActivity(intent)
        }
    }

    private fun initData() {
        db.viewModel?.initStoryInfo({
            checkStatus()
            if (!AppContext.isAuthor()) {
                return@initStoryInfo
            }
            val isPublished = db.viewModel?.story?.value?.getIsPublished()
            if (isPublished != null && isPublished == true) {
                db.btnPublish.visibility = View.GONE
            } else {
                db.btnPublish.visibility = View.VISIBLE
            }
        },
            {
                DialogUtils.showLongMessage(
                    this@StoryDescriptionActivity,
                    R.string.tip_load_story_failed
                )
            })
    }

    private fun checkStatus() {
        checkCollect()
        checkIsAuthor()
    }

    private fun checkCollect() {
        db.viewModel?.checkCollected {
            refreshCollectButton(it)
        }
    }

    private fun checkIsAuthor() {
        db.viewModel?.checkIsAuthor {
            checkShowAuthorInfo(it)
        }
    }

    private fun refreshCollectButton(b: Boolean) {
        if (b) {
            db.btnCollect.setText(R.string.btn_dis_collect)
        } else {
            db.btnCollect.setText(R.string.btn_collect)
        }
    }

    private fun checkShowAuthorInfo(b: Boolean) {
        if (b) {
            db.rv.visibility = View.VISIBLE
        } else {
            db.rv.visibility = View.GONE
        }
    }

    private fun onRead() {
        db.viewModel?.getFirstPage {
            if (it == -1) {
                DialogUtils.showLongMessage(this@StoryDescriptionActivity, R.string.read_story_failed)
            } else {
                PageActivity.startReadInstance(this@StoryDescriptionActivity, it)
            }
        }
    }

    private fun onEdit() {
        db.viewModel?.story?.value?.getStoryId()?.let {
            CreateStoryActivity.startEditInstance(
                this@StoryDescriptionActivity,
                it
            )
        }
    }

    private fun showResult(isSuccess: Boolean) {
        val id: Int
        if (isSuccess) {
            id = R.string.tip_success
            DialogUtils.showLongMessage(this@StoryDescriptionActivity, id)
            finish()
        } else {
            id = R.string.tip_failed
            DialogUtils.showLongMessage(this@StoryDescriptionActivity, id)
        }
    }

    private fun showCollectResult(isSuccess: Boolean) {
        val id: Int
        if (isSuccess) {
            id = R.string.tip_success
            DialogUtils.showLongMessage(this@StoryDescriptionActivity, id)
            initData()
        } else {
            id = R.string.tip_failed
            DialogUtils.showLongMessage(this@StoryDescriptionActivity, id)
        }
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initData()
    }

    override fun initView() {
        showBackTitle()
        RecycleViewUtils.addLLLayoutManager(this@StoryDescriptionActivity, db.rv)
        if (AppContext.isAuthor()) {
            db.btnDelete.visibility = View.VISIBLE
            db.btnEdit.visibility = View.VISIBLE
            db.btnCollect.visibility = View.GONE
            val isPublished = db.viewModel?.story?.value?.getIsPublished()
            if (isPublished != null && isPublished == false) {
                db.btnPublish.visibility = View.GONE
            } else {
                db.btnPublish.visibility = View.VISIBLE
            }
        } else {
            db.btnDelete.visibility = View.GONE
            db.btnEdit.visibility = View.GONE
            db.btnPublish.visibility = View.GONE
            db.viewModel?.checkCollect {
                if (it) {
                    db.btnUnCollect.visibility = View.VISIBLE
                    db.btnCollect.visibility = View.GONE
                } else {
                    db.btnUnCollect.visibility = View.GONE
                    db.btnCollect.visibility = View.VISIBLE
                }
            }
        }
        db.btnEdit.setOnClickListener { onEdit() }
        db.btnCollect.setOnClickListener { db.viewModel?.onCollect(::showCollectResult) }
        db.btnUnCollect.setOnClickListener { db.viewModel?.onCollect(::showCollectResult) }
        db.btnDelete.setOnClickListener {
            DialogUtils.showConfirmDialog(this@StoryDescriptionActivity) {
                db.viewModel?.onDelete(::showResult)
            }
        }
        db.btnRead.setOnClickListener { onRead() }
        db.btnPublish.setOnClickListener {
            DialogUtils.showConfirmDialog(
                this@StoryDescriptionActivity,
                getString(R.string.message_confirm_publish)
            ) {
                db.viewModel?.onPublish(::showResult)
            }
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_story_description_layout
    }

    override fun getViewModel(): StoryDescriptionViewModel {
        return ViewModelProvider(
            this, ViewModelProvider
                .AndroidViewModelFactory(application)
        )[StoryDescriptionViewModel::class.java]
    }

    override fun setViewModel() {
        db.viewModel = getViewModel()
    }
}